<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qquickpackage.cpp -->
  <title>Package QML 类型| Qt QML 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtqml-index.html">Qt QML</a></td><td ><a href="qtqml-qmlmodule.html">QML Types</a></td><td >Package QML 类型</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtqml-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">目录</a></h3>
<ul>
<li class="level1"><a href="#attached-properties">附加属性</a></li>
<li class="level1"><a href="#details">详细描述</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">QML的Package类型</h1>
<span class="subtitle"></span>
<!-- $$$Package-brief -->
<p>指定被命名项的集合。<a href="#details">更多…</a></p>
<!-- @@@Package -->
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> 导入语句:</td><td class="memItemRight bottomAlign"> import  .</td></tr></table></div><ul>
<li><a href="qml-package-members.html">所有成员的列表，包括继承的成员</a></li>
</ul>
<a name="attached-properties"></a>
<h2 id="attached-properties">附加属性</h2>
<ul>
<li class="fn"><b><b><a href="qml-package.html#name-attached-prop">name</a></b></b> : string</li>
</ul>
<!-- $$$Package-description -->
<a name="details"></a>
<h2 id="details">详细描述</h2>
<p>Package类型与<a href="qml-qtqml-models-delegatemodel.html">DelegateModel</a>一起使用，使具有共享上下文的委托能够提供给多个视图。</p>
<p>Package中的任何项都可以通过 <a href="qml-package.html#name-attached-prop">Package.name</a>附加属性指定名称。</p>
<p>下面的示例创建一个包含两个命名项（<i>list</i> 和 <i>grid</i>）的包：包中的第三个项（<a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a>）是应该出现在哪个委托中的父类。这允许项在视图之间移动。</p>
<pre class="qml">

  <span class="type"><a href="qml-package.html">Package</a></span> {
      <span class="type"><a href="../qtquick/qml-qtquick-text.html">Text</a></span> { <span class="name">id</span>: <span class="name">listDelegate</span>; <span class="name">width</span>: <span class="name">parent</span>.<span class="name">width</span>; <span class="name">height</span>: <span class="number">25</span>; <span class="name">text</span>: <span class="string">'Empty'</span>; <span class="name">Package</span>.name: <span class="string">'list'</span> }
      <span class="type"><a href="../qtquick/qml-qtquick-text.html">Text</a></span> { <span class="name">id</span>: <span class="name">gridDelegate</span>; <span class="name">width</span>: <span class="name">parent</span>.<span class="name">width</span> <span class="operator">/</span> <span class="number">2</span>; <span class="name">height</span>: <span class="number">50</span>; <span class="name">text</span>: <span class="string">'Empty'</span>; <span class="name">Package</span>.name: <span class="string">'grid'</span> }

      <span class="type"><a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a></span> {
          <span class="name">id</span>: <span class="name">wrapper</span>
          <span class="name">width</span>: <span class="name">parent</span>.<span class="name">width</span>; <span class="name">height</span>: <span class="number">25</span>
          <span class="name">color</span>: <span class="string">'lightsteelblue'</span>

          <span class="type"><a href="../qtquick/qml-qtquick-text.html">Text</a></span> { <span class="name">text</span>: <span class="name">display</span>; <span class="name">anchors</span>.centerIn: <span class="name">parent</span> }
          <span class="name">state</span>: <span class="name">root</span>.<span class="name">upTo</span> <span class="operator">&gt;</span> <span class="name">index</span> ? <span class="string">'inGrid'</span> : <span class="string">'inList'</span>
          <span class="name">states</span>: [
              <span class="type"><a href="qml-qtqml-statemachine-state.html">State</a></span> {
                  <span class="name">name</span>: <span class="string">'inList'</span>
                  <span class="type"><a href="../qtquick/qml-qtquick-parentchange.html">ParentChange</a></span> { <span class="name">target</span>: <span class="name">wrapper</span>; <span class="name">parent</span>: <span class="name">listDelegate</span> }
              },
              <span class="type"><a href="qml-qtqml-statemachine-state.html">State</a></span> {
                  <span class="name">name</span>: <span class="string">'inGrid'</span>
                  <span class="type"><a href="../qtquick/qml-qtquick-parentchange.html">ParentChange</a></span> {
                      <span class="name">target</span>: <span class="name">wrapper</span>; <span class="name">parent</span>: <span class="name">gridDelegate</span>
                      <span class="name">x</span>: <span class="number">0</span>; <span class="name">y</span>: <span class="number">0</span>; <span class="name">width</span>: <span class="name">gridDelegate</span>.<span class="name">width</span>; <span class="name">height</span>: <span class="name">gridDelegate</span>.<span class="name">height</span>
                  }
              }
          ]

          <span class="name">transitions</span>: [
              <span class="type"><a href="../qtquick/qml-qtquick-transition.html">Transition</a></span> {
                  <span class="type"><a href="../qtquick/qml-qtquick-parentanimation.html">ParentAnimation</a></span> {
                      <span class="type"><a href="../qtquick/qml-qtquick-numberanimation.html">NumberAnimation</a></span> { <span class="name">properties</span>: <span class="string">'x,y,width,height'</span>; <span class="name">duration</span>: <span class="number">300</span> }
                  }
              }
          ]
      }
  }

</pre>
<p>这些命名项被引用特殊的<a href="qml-qtqml-models-delegatemodel.html#parts-prop">DelegateModel::parts</a>属性的两个视图用作委托，以选择提供所选委托的模型。</p>
<pre class="qml">

  <span class="type"><a href="qml-qtqml-models-delegatemodel.html">DelegateModel</a></span> {
      <span class="name">id</span>: <span class="name">visualModel</span>
      <span class="name">delegate</span>: <span class="name">Delegate</span> {}
      <span class="name">model</span>: <span class="name">myModel</span>
  }

  <span class="type"><a href="../qtquick/qml-qtquick-listview.html">ListView</a></span> {
      <span class="name">id</span>: <span class="name">lv</span>
      <span class="name">height</span>: <span class="name">parent</span>.<span class="name">height</span><span class="operator">/</span><span class="number">2</span>
      <span class="name">width</span>: <span class="name">parent</span>.<span class="name">width</span>

      <span class="name">model</span>: <span class="name">visualModel</span>.<span class="name">parts</span>.<span class="name">list</span>
  }
  <span class="type"><a href="../qtquick/qml-qtquick-gridview.html">GridView</a></span> {
      <span class="name">y</span>: <span class="name">parent</span>.<span class="name">height</span><span class="operator">/</span><span class="number">2</span>
      <span class="name">height</span>: <span class="name">parent</span>.<span class="name">height</span><span class="operator">/</span><span class="number">2</span>
      <span class="name">width</span>: <span class="name">parent</span>.<span class="name">width</span>
      <span class="name">cellWidth</span>: <span class="name">width</span> <span class="operator">/</span> <span class="number">2</span>
      <span class="name">cellHeight</span>: <span class="number">50</span>
      <span class="name">model</span>: <span class="name">visualModel</span>.<span class="name">parts</span>.<span class="name">grid</span>
  }

</pre>
<p><b>参见</b><a href="../qtquick/qtquick-views-example.html">Qt Quick例子 - Views（视图）</a>, <a href="../qtdoc/qtdoc-demos-photoviewer-example.html">Qt Quick演示 - Photo Viewer（图片查看器）</a>以及<a href="qtqml-index.html">Qt QML</a>。</p>
<!-- @@@Package -->
<h2>附加属性文档</h2>
<!-- $$$name -->
<div class="qmlitem"><div class="qmlproto">
<div class="table"><table class="qmlname">
<tr valign="top" class="odd" id="name-attached-prop">
<td class="tblQmlPropNode"><p>
<a name="name-attached-prop"></a><span class="name">Package.name</span> : <span class="type"><a href="qml-string.html">string</a></span></p></td></tr>
</table></div>
</div><div class="qmldoc"><p>此附加属性保存包中项的名称。</p>
</div></div><!-- @@@name -->
<br/>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
